package com.peas.platform.common.core.configuration;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.common.core.enums.BaseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;

@Slf4j
public class ConverterBaseEnum<E extends BaseEnum> implements Converter<String, E> {

	@SuppressWarnings("unchecked")
	@Override
	public E convert(@NonNull String source) {
		log.debug("ConverterBaseEnum, source:" + source);
		if (StrUtil.isBlank(source)) {
			return null;
		}
		Class<E> clazz = (Class<E>) ClassUtil.getTypeArgument(getClass(), 1);
		return BaseEnum.key2Enum(clazz, source);
	}

}
